﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communication;
using Clipper.Model;
using System.Threading;

namespace Clipper.BLL.DetectChain.AddR1AndSample
{
    public class ArmReagent1 : CupComponent
    {
        //试剂臂R1A
        public static ArmControl R1A = AddReagent1AndSample.R1A;
        /// <summary>
        /// 试剂臂R1B
        /// </summary>
        public static ArmControl R1B = AddReagent1AndSample.R1B;

        //样本臂SA
        public static ArmControl SA = AddReagent1AndSample.SA;
        //样本臂SB
        public static ArmControl SB = AddReagent1AndSample.SB;

        static ControlStatusEnum detectionStatus;
        /// <summary>
        /// 模块状态
        /// </summary>
        public override Model.ControlStatusEnum DetectionStatus
        {
            get
            {
                return detectionStatus;
            }
            set
            {
                detectionStatus = value;
            }
        }

        public override void Run(Model.ReactionCup cup)
        {
            DetectionStatus = ControlStatusEnum.BUSY;

            if (R1A.ControlStatus == ControlStatusEnum.IDLE)
            {
                R1A.ControlStatus = ControlStatusEnum.BUSY;
                R1SampleHelper.armR1 = R1A;
            }
            else if (R1B.ControlStatus == ControlStatusEnum.IDLE)
            {
                R1B.ControlStatus = ControlStatusEnum.BUSY;
                R1SampleHelper.armR1 = R1B;
            }
            if (SA.ControlStatus == ControlStatusEnum.IDLE)
            {
                SA.ControlStatus = ControlStatusEnum.BUSY;
                R1SampleHelper.armSample = SA;
            }
            else if (SB.ControlStatus == ControlStatusEnum.IDLE)
            {
                SB.ControlStatus = ControlStatusEnum.BUSY;
                R1SampleHelper.armSample = SB;
            }
            //试剂1A吸液
            R1SampleHelper.armR1.Imbibe();
            //样本臂SA

            R1SampleHelper.armR1.WaitStatus(1);
            R1SampleHelper.armR1.Inject();
            R1SampleHelper.armR1.WaitStatus(2);
            R1SampleHelper.armR1.Rinse();//清洗
            R1SampleHelper.armR1.WaitRinseTime();
            AddReagent1AndSample.Kit1.Reset();//复位试剂盘
            AddReagent1AndSample.Kit1.WaitStatus(0);
            while (true)
            {
                if (NextComponent.DetectionStatus == ControlStatusEnum.IDLE)
                    break;
                else
                Thread.Sleep(100);
            }

            NextComponent.DetectionStatus = ControlStatusEnum.BUSY;
            DetectionStatus = ControlStatusEnum.IDLE;
            NextComponent.Run(cup);
        }
    }
}
